package cn.beforedark.yygh.userinfo.controller;


import cn.beforedark.yygh.common.result.R;
import cn.beforedark.yygh.common.util.JwtHelper;
import cn.beforedark.yygh.model.userinfo.Patient;
import cn.beforedark.yygh.userinfo.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * /user/userinfo/patient
 */
@RestController
@RequestMapping("/user/userinfo/patient")
public class PatientController {

    @Autowired
    private PatientService patientService;

    /**
     * 保存受诊人信息
     *
     * @param patient patient
     * @param token   token
     * @return vo
     */
    @PostMapping("/save")
    public R save(@RequestBody Patient patient, @RequestHeader String token) {
        Long userId = JwtHelper.getUserId(token);
        patient.setUserId(userId);
        patientService.save(patient);
        return R.ok();
    }

    /**
     * 删除受诊人信息
     *
     * @param id 受诊人id
     * @return vo
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") Long id) {
        patientService.removeById(id);
        return R.ok();
    }

    /**
     * 详情
     *
     * @param id id
     * @return vo
     */
    @GetMapping("/detail/{id}")
    public R detail(@PathVariable Long id) {
        Patient patient = patientService.detail(id);
        return R.ok().data("patient", patient);
    }

    /**
     * 更新
     *
     * @param patient patient
     * @return vo
     */
    @PutMapping("/update")
    public R update(@RequestBody Patient patient) {
        patient.setUpdateTime(new Date());
        patientService.updateById(patient);
        return R.ok();
    }


    /**
     * 查看受诊人详情
     *
     * @param token token
     * @return vo
     */
    @GetMapping("/list")
    public R findAll(@RequestHeader String token) {
        List<Patient> list = patientService.findAll(token);
        return R.ok().data("list", list);
    }
}
